package com.javaweb.service.impl;

import com.javaweb.common.BusinessConstants;
import com.javaweb.dao.ArticleDao;
import com.javaweb.dao.UserDao;
import com.javaweb.model.Article;
import com.javaweb.service.ArticleService;
import com.javaweb.service.dto.ArticleConditionDto;
import com.javaweb.service.dto.ArticleUpdateDto;
import com.javaweb.service.dto.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/10/16 9:47
 */

@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private UserDao userDao ;
    @Autowired
    private ArticleDao articleDao ;

    public void method() {
        this.publishArticle("1","1") ;
    }


    @Transactional
    @Override
    public int publishArticle(String userId, String articleId) {

        Integer ret =  articleDao.updateStatus(Integer.valueOf(articleId),
                               BusinessConstants.ARTICLE_PUBLISHED) ;
        if (ret > 0) {
            ret = userDao.updateScore(Integer.valueOf(userId),
                    BusinessConstants.ARTICLE_SCORE) ;
        } else {
            log.error("publish article : update status failed." ) ;
            return 0 ;
        }
        try {
            if (true) {
                throw new RuntimeException("exception.");
            }
        } catch (Exception e) {
            log.error(e.getMessage()) ;
        }
        log.info("publish article successfully." ) ;
        return ret;
    }

    @Override
    public PageResult queryByArticleCondition(ArticleConditionDto articleCondition, Integer pageNum, Integer pageSize) {
        return null;
    }

    @Override
    public Article queryById(Integer articleId) {
        return  articleDao.queryById(articleId);
    }

    @Override
    public Article insertArticle(Article article) {
        int ret = articleDao.insert(article);
            Article article1 = articleDao.queryById(ret);
            return article1;
    }

    @Transactional
    @Override
    public int publishArticlesByBatch(String[] articleIds, String userId) {
        int i =0;
        Integer ret = 0;
        while (i<articleIds.length){
            ret =  articleDao.updateStatus(Integer.valueOf(articleIds[i]),
                    BusinessConstants.ARTICLE_PUBLISHED) ;
            if (ret > 0) {
                ret = userDao.updateScore(Integer.valueOf(userId),
                        BusinessConstants.ARTICLE_SCORE) ;
            } else {
                log.error("publish article : update status failed." ) ;
                return 0 ;
            }
            try {
                if (true) {
                    throw new RuntimeException("exception.");
                }
            } catch (Exception e) {
                log.error(e.getMessage()) ;
            }
            log.info("publish article successfully." ) ;
            i++;
        }

        return ret;
    }

    @Transactional
    @Override
    public int removeArticle(String articleId, String userId) {
        Integer ret =  articleDao.updateStatus(Integer.valueOf(articleId),
                BusinessConstants.ARTICLE_DRAFTED) ;
        if (ret > 0) {
            ret = userDao.updateScore(Integer.valueOf(userId),
                    BusinessConstants.ARTICLE_REMOVE_SCORE) ;
        } else {
            log.error("remove article : update status failed." ) ;
            return 0 ;
        }
        try {
            if (true) {
                throw new RuntimeException("exception.");
            }
        } catch (Exception e) {
            log.error(e.getMessage()) ;
        }
        log.info("remove article successfully." ) ;
        return ret;
    }


    @Transactional
    @Override
    public int updateArticle(ArticleUpdateDto articleUpdateDto) {
        Integer ret = articleDao.updateByArticleUpdateDto(articleUpdateDto);

        if (ret>0){
            ret = articleDao.updateByArticleUpdateDtoSetTagsId(articleUpdateDto);
        }else {
            log.error("update article : update TagsId failed." ) ;
            return 0 ;
        }
        try {
            if (true) {
                throw new RuntimeException("exception.");
            }
        } catch (Exception e) {
            log.error(e.getMessage()) ;
        }
        log.info("update article successfully." ) ;
        return ret;
    }
}
